#include <bits/stdc++.h>

using namespace std;

#define in read()

const int MAXN = 510;
const int MAXQ = 3e5+10;

int tr[MAXN][MAXN],n,q,all,maxx;
int ans[MAXQ],a[MAXN][MAXN];

struct aam{
	int o,x,y,X,Y,k;
}s[MAXQ],lts[MAXQ],rts[MAXQ];

int read(){
	int x = 0,sgn = 1;char ch = getchar();
	for(;!isdigit(ch);ch = getchar()) if(ch == '-') sgn = -1;
	for(;isdigit(ch);ch = getchar()) x = (x<<1) + (x<<3) + (ch ^ 48);
	return x * sgn;
}

void add(int x,int y,int num){
	for(;x <= n;x += x & -x){
		int ty = y;
		for(;ty <= n;ty += ty & -ty)
			tr[x][ty] += num;
	}
}

int query(int x,int y){
	int res = 0;
	for(;x;x -= x & -x){
		int ty = y;
		for(;ty;ty -= ty & -ty)
			res += tr[x][ty];
	}
	return res;
}

void solve(int sl,int sr,int lval,int rval){
	if(sl > sr || lval > rval) return;
	if(lval == rval){
		for(int i = sl;i <= sr;i++){
			if(s[i].o) ans[s[i].o] = lval;
		}
		return;
	}
	int mid = lval + rval >> 1;
	int lnum = 0,rnum = 0;
	for(int i = sl;i <= sr;i++){
		if(s[i].o){
			int tot = query(s[i].X,s[i].Y) - query(s[i].X,s[i].y-1) - query(s[i].x-1,s[i].Y) + query(s[i].x-1,s[i].y-1);
			if(tot < s[i].k) s[i].k -= tot,rts[++rnum] = s[i];
			else lts[++lnum] = s[i];
		}
		else{
			if(s[i].k <= mid) add(s[i].x,s[i].y,1),lts[++lnum] = s[i];
			else rts[++rnum] = s[i];
		}
	}
	for(int i = sl;i <= sr;i++)
		if(!s[i].o && s[i].k <= mid) add(s[i].x,s[i].y,-1);
	for(int i = 1;i <= lnum;i++) s[sl + i - 1] = lts[i];
	for(int i = 1;i <= rnum;i++) s[sl + lnum + i - 1] = rts[i];
	solve(sl,sl+lnum-1,lval,mid); solve(sl+lnum,sr,mid+1,rval);
}

int main (){
	n = in,q = in;
	for(int i = 1;i <= n;i++)
		for(int j = 1;j <= n;j++){
			a[i][j] = in; maxx = max(a[i][j],maxx);
			s[++all] = (aam){0,i,j,0,0,a[i][j]};
		}
	for(int i = 1;i <= q;i++) s[++all] = (aam){i,in,in,in,in,in};
	solve(1,all,1,maxx);
	for(int i = 1;i <= q;i++) printf("%d\n",ans[i]);
	return 0;
}
